/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.commons.accessor.properties;

import java.util.HashMap;

/**
 * 文字列から数値と真偽値への変換
 * @author aileron
 *
 */
public class PojoNumberConvertUtils
{
    /**
     * 変換用インタフェース
     * @author aileron
     *
     */
    private static interface Convert <T>
    {
        /**
         * 変換
         * @param <T>
         * @param t
         * @param value
         * @return
         */
        T convert(Number value);
    }
    
    /**
     * convert-map
     */
    private static final HashMap<Class<?>,Convert<?>> map =
        new HashMap<Class<?>, Convert<?>>();
    static
    {

        map.put(Byte.TYPE, new Convert<Byte>()
                {
                    public Byte convert(Number value)
                    {
                        return value.byteValue();
                    }
                });


        map.put(Short.TYPE, new Convert<Short>()
                {
                    public Short convert(Number value)
                    {
                        return value.shortValue();
                    }
                });


        map.put(Integer.TYPE, new Convert<Integer>()
                {
                    public Integer convert(Number value)
                    {
                        return value.intValue();
                    }
                });


        map.put(Long.TYPE, new Convert<Long>()
                {
                    public Long convert(Number value)
                    {
                        return value.longValue();
                    }
                });


        map.put(Float.TYPE, new Convert<Float>()
                {
                    public Float convert(Number value)
                    {
                        return value.floatValue();
                    }
                });


        map.put(Double.TYPE, new Convert<Double>()
                {
                    public Double convert(Number value)
                    {
                        return value.doubleValue();
                    }
                });
        
        map.put(Boolean.TYPE, new Convert<Boolean>()
                {
                    public Boolean convert(Number value)
                    {
                        return value.intValue()!=0;
                    }
                });
        
        map.put(Byte.class, new Convert<Byte>()
                {
                    public Byte convert(Number value)
                    {
                        return value.byteValue();
                    }
                });


        map.put(Short.class, new Convert<Short>()
                {
                    public Short convert(Number value)
                    {
                        return value.shortValue();
                    }
                });


        map.put(Integer.class, new Convert<Integer>()
                {
                    public Integer convert(Number value)
                    {
                        return value.intValue();
                    }
                });


        map.put(Long.class, new Convert<Long>()
                {
                    public Long convert(Number value)
                    {
                        return value.longValue();
                    }
                });


        map.put(Float.class, new Convert<Float>()
                {
                    public Float convert(Number value)
                    {
                        return value.floatValue();
                    }
                });


        map.put(Double.class, new Convert<Double>()
                {
                    public Double convert(Number value)
                    {
                        return value.doubleValue();
                    }
                });
        
        map.put(Boolean.class, new Convert<Boolean>()
                {
                    public Boolean convert(Number value)
                    {
                        return value.intValue()!=0;
                    }
                });
    }
    
    /**
     * 変換
     * @param t
     * @param value
     * @return
     */
    public static Object convertNumber(Class<?> t, Number value)
    {
        final Convert<?> convert = map.get(t);
        if(convert==null)
        {
            return null;
        }
        return convert.convert(value);
    }
}